/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package meta;

import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Risto Laakso <risto.laakso@gmail.com>
 */
public class MetaInfo {

	public Map<String,String> manifest = new HashMap<String, String>();
	public Map<String,String[]> testsuite = new HashMap<String, String[]>();
	public Map<String,String[]> coverage = new HashMap<String, String[]>();

	public MetaInfo(Class cls) throws IOException, ParserConfigurationException, SAXException {

		//System.out.println("Try " + cls.getSimpleName() + ".class");
		URL me = cls.getResource(cls.getSimpleName()+".class");
		if (me == null) throw new IOException("Class not found!");
		
		JarFile jf = ((JarURLConnection) me.openConnection()).getJarFile();
		System.out.println("JarFile " + jf);

		Manifest mf = jf.getManifest();
		for (Object k : mf.getMainAttributes().keySet()) {
			String a = k.toString();
			String b = mf.getMainAttributes().get(k).toString();
			manifest.put(a,b);
			System.out.println(k + " -> "+ mf.getMainAttributes().get(k));
		}

		Enumeration<JarEntry> em = jf.entries();
		while (em.hasMoreElements()) {
			JarEntry je = em.nextElement();
			if (je.getName().startsWith("META-INF/") && je.getName().endsWith(".xml")) {
				System.out.println(je.getName());
				ZipEntry ze = jf.getEntry(je.getName());
				InputStream is = jf.getInputStream(ze);
				DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				Document doc = db.parse(is);
				System.out.println(doc.getDocumentElement());

				// COVERAGE
				NodeList pList = doc.getElementsByTagName("package");
				for (int i = 0; i < pList.getLength(); i++) {
					Node n = pList.item(i);

					List<String> lst = new ArrayList<String>();
					String name = n.getAttributes().getNamedItem("name").getNodeValue();
					lst.add(name);

					NodeList cList = n.getChildNodes();
					for (int j = 0; j < cList.getLength(); j++) {
						Node m = cList.item(j);
						if (m.getNodeName().equals("coverage")) {
							String type = m.getAttributes().getNamedItem("type").getNodeValue();
							String value = m.getAttributes().getNamedItem("value").getNodeValue();
							lst.add(type);
							lst.add(value);
//							System.out.println(type + " = " + value);
						}
					}
					coverage.put(name, lst.toArray(new String[lst.size()]));
				}

				// TESTSUITE
				if (doc.getDocumentElement().getNodeName().equals("testsuite")) {
					NamedNodeMap attr = doc.getDocumentElement().getAttributes();
					String err = attr.getNamedItem("errors").getNodeValue();
					String fail = attr.getNamedItem("failures").getNodeValue();
					String test = attr.getNamedItem("tests").getNodeValue();
					String name = attr.getNamedItem("name").getNodeValue();
					String time = attr.getNamedItem("time").getNodeValue();
					String timestamp = attr.getNamedItem("timestamp").getNodeValue();
					System.out.println(name + " -> " + err + ", " + fail + ", " + test + ", " + time + ", " + timestamp);
					testsuite.put(name, new String[]{ test, err, fail, time, timestamp });
				}

			}
		}
	}


}
